/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shattered2;

import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author 10135228
 */
public class MainFrame extends javax.swing.JFrame {

    DrawPanel2 zoomx = new DrawPanel2(this,1);
    //HashMap<String, ArrayList<PuzzlePiece>>[] codeTables;
    PuzzlePiece[] pieces = null;
    static final int TOP = 0, LEFT = 1, BOTTOM = 2, RIGHT = 3;
    private PuzzleField fieldData = null;
    PiecesWindow piecesWindow = null;
    

    public PuzzleField getFieldData() {
        return fieldData;
    }
    /**
     * Creates new form MainFrame
     */
    public MainFrame(PuzzlePiece[] pieces) {
        this.pieces = pieces;
        this.setTitle("Puzzle");
        fieldData = new PuzzleField();
        fieldData.addDisplay(zoomx);
        //fieldData.setPieceAt(2, 2, pieces[0]);//test
        //fieldData.setPieceAt(2, 3, pieces[1]);//test
        initComponents();
        this.setSize(1280, 1024);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_SPACE)
                jButton6.doClick();               
            }

            @Override
            public void keyTyped(KeyEvent e) {
                if(piecesWindow != null)
                piecesWindow.keyTyped(e);
            }
            
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton5 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton6 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane1.setViewportView(zoomx);
        StatusLabel = new javax.swing.JLabel();

        FormListener formListener = new FormListener();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jLabel1.setText("Zoom: ");
        jToolBar1.add(jLabel1);

        jButton1.setText("1x");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(formListener);
        jToolBar1.add(jButton1);

        jButton2.setText("2x");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(formListener);
        jToolBar1.add(jButton2);

        jButton3.setText("4x");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(formListener);
        jToolBar1.add(jButton3);

        jButton4.setText("8x");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(formListener);
        jToolBar1.add(jButton4);
        jToolBar1.add(jSeparator1);

        jButton5.setText("Save");
        jButton5.setFocusable(false);
        jButton5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(formListener);
        jToolBar1.add(jButton5);
        jToolBar1.add(jSeparator2);

        jButton6.setText("Minimize");
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton6.addActionListener(formListener);
        jToolBar1.add(jButton6);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);
        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        StatusLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        StatusLabel.setText("Status");
        StatusLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        StatusLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        getContentPane().add(StatusLabel, java.awt.BorderLayout.SOUTH);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == jButton1) {
                MainFrame.this.jButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton2) {
                MainFrame.this.jButton2ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton3) {
                MainFrame.this.jButton3ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton4) {
                MainFrame.this.jButton4ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton5) {
                MainFrame.this.jButton5ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton6) {
                MainFrame.this.jButton6ActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        zoomx.setZoom(8);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        zoomx.setZoom(1);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        zoomx.setZoom(2);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        zoomx.setZoom(4);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        fieldData.save();
        StatusLabel.setText("Saved");
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        this.setExtendedState(JFrame.ICONIFIED);
        if(piecesWindow != null){
            piecesWindow.setExtendedState(JFrame.ICONIFIED);
        }
        
    }//GEN-LAST:event_jButton6ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel StatusLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    void MouseClickedOnPanel(int x, int y) {
        StatusLabel.setText("("+x+"-"+y+")");
        if(this.piecesWindow==null){
            piecesWindow = new PiecesWindow("Pieces",fieldData,pieces);
            piecesWindow.setSize(1280,1024);
            if(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length>1){
                piecesWindow.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration().getBounds().width, 0);
            }
            piecesWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);            
        }
        if(!this.piecesWindow.isVisible()){
            piecesWindow.setVisible(true);
        }
        if(this.piecesWindow.getState()==JFrame.ICONIFIED){
            piecesWindow.setState(JFrame.NORMAL);
        }
        piecesWindow.updatePieces(x,y);
       
    }
}
